\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,xparse,calc}
\RequirePackage{xifthen}
\GetIdInfo$Id: postgraduateiii.dtx v3.1.9(testing)  2019-10-08  ZhenhuaFeng  <fengzhenhua@outlook.com> $ {For Chinese Postgraduate Entrance Examination}
\ProvidesExplPackage{\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
%总页码计数器
\int_new:N \post_page_int
\int_new:N \post_page_i_int
\int_new:N \post_totalpage_int
\int_new:N \post_section_int
% 判断章命令否否存在
\cs_if_exist:NTF \chapter
{\relax}
{\newcounter{chapter}}
% 在LaTeX2e版本中加入了对新的总页码定义前进行判断的命令，这样可以保证如果输出的页码是错乱了只定义出最大的。但是经测试在LaTeX3中，这个顺序是可以保证，这样的话再判断页码大小就显得多余了，而且这样的总页码的数量是较多的，所以对其精简是相当重要的，因此在顺序保证后就可以修改为一条定义总页码的命令。
\DeclareRobustCommand\MakePage[2]{%
   \exp_args:No \cs_set:Nn {\cs:w total_int_#1 :\cs_end:}{#2}
}%
%定义总页码
\cs_new:Nn \cexam_page_total:
{
   \exp_args:No \cs_if_exist:NTF 
   {\cs:w total_int_\int_use:N\c@chapter\int_use:N\c@section\int_use:N\post_section_int :\cs_end:}
   {\cs:w total_int_\int_use:N\c@chapter\int_use:N\c@section\int_use:N\post_section_int :\cs_end:}
   {\ ??}
}
% 定义总页码写出,文件引入应当放在open之前，在latex3中，如果在open之后引入文件，则不执行。
\iow_new:N \cexam_page
\file_if_exist_input:n{\jobname.page}
\iow_open:Nn \cexam_page{\jobname.page}
%定义格式
\cs_new:Nn \post_odd_foot:
{
   \int_gset:Nn \post_page_i_int {\post_page_int}
   \parbox{\linewidth}{
      \rule[5pt]{\linewidth}{.4pt}
      \newline
      科目名称：\post_class:
      \hfill 第\int_use:N\post_page_int{}页 \quad{}共\cexam_page_total:{}页
      \iow_shipout_x:Nn\cexam_page 
      {\MakePage{\int_use:N\c@chapter\int_use:N\c@section\int_use:N\post_section_int}{\int_use:N\post_page_i_int}}
   }
   \int_gadd:Nn \post_page_int{1}
}
\cs_new:Nn \post_odd_head:{\hfill\thepage}
\cs_new:Nn \post_even_head:{\thepage\hfill}
\cs_new:Nn \post_even_foot:{\post_odd_foot:}
\cs_new:Nn \post_ps_format:{
   \tex_let:D\@oddhead\post_odd_head:
   \tex_let:D\@oddfoot\post_odd_foot:
   \tex_let:D\@evenhead\post_even_head:
   \tex_let:D\@evenfoot\post_even_foot:
}
\tex_let:D\ps@post\post_ps_format:
% 定义1学校，2科目
\cs_new:Nn \post_school: {school}
\cs_new:Nn \post_class:{class}
\cs_new:Nn \post_year: {1986}
\cs_new:Nn \post_score: {150}
\cs_new:Nn \post_time: {180}
%
\bool_new:N \post_post_bool
%
\DeclareRobustCommand\postgraduate[2]
{%
   \newpage
   \pagestyle{post}
   \bool_set_true:N\post_post_bool
   \cs_set:Nn \post_school: {#1}
   \cs_set:Nn \post_class: {#2}
   \cs_if_exist:NTF \chapter
   {\addcontentsline{toc}{chapter}{#1}}
   {\addcontentsline{toc}{section}{#1}}
   \cexam_answer_add:n \postgraduate{#1}{#2（答案）}\scan_stop:
}
%
\DeclareRobustCommand\postschool[1]
{
   \cs_set:Nn \post_school: {#1}
   \cexam_answer_add:n \postschool{#1}\scan_stop:
}
\DeclareRobustCommand\postclass[1]
{
   \cs_set:Nn \post_class: {#1}
   \cexam_answer_add:n \postclass{#1（答案）}\scan_stop:
}
%
\int_new:N \post_notice_int
%
\DeclareRobustCommand\setpostnotice[1]{\int_set:Nn \post_notice_int {#1}}
%
\DeclareRobustCommand\postnotice[1]
{
   \int_add:Nn \post_notice_int{1}
   \exp_args:No \cs_set:Nn {\cs:w post_notice_\int_use:N\post_notice_int :\cs_end:}{#1}
   \cexam_answer_add:n \setpostnotice{\int_use:N\post_notice_int}\scan_stop:
}
%
\DeclareRobustCommand\postyear[1]
{
   \bool_if:NTF \post_post_bool
   {\bool_set_false:N\post_post_bool}
   {\newpage}
   \int_gset:Nn \post_page_int{1}
   \int_add:Nn \post_section_int{1}
   {\parindent=0pt
      \begin{center}
	 \large\bf%
	 \post_school:\\[.4\ccwd]
	 {#1}年招收硕士研学位研究生入学统一考试试题\\[.4\ccwd]
	 科目名称：\post_class:
      \end{center}
      考生须知：\cs:w post_notice_\int_use:N\post_notice_int :\cs_end:
      \rule[5pt]{\linewidth}{.4pt}
      \cs_if_exist:NTF \chapter
      {\addcontentsline{toc}{section}{#1年\post_class:}}
      {\addcontentsline{toc}{subsection}{#1年\post_class:}}
   }
   \cexam_answer_add:n \postyear{#1}\scan_stop:
}
%% 暂时追加一部分题目输入格式定义 
\newcounter{cex@mproblem}
\cs_new:Nn \cexam_problem: {TotalNumber}
\cs_new:Nn \cexam_score: {TotalScore}
\cs_new:Nn \cexam_score_i: {EveryScore}
\dim_new:N \cexam_problem_dim
\dim_new:N \cexam_pb_indent_dim
\cs_new:Nn \cexam_problem_i: 
{
   {\bf \chinese{cex@mproblem}、选择题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。请从题目所列出的选项中选择一正确的项填充空格。每题的四个备选项中只有一个是正确的，不选、错选或多选均不得分。请将你的选择标清题号写在考场发的答题纸上，直接填写在试题上无效。）
}
\cs_new:Nn \cexam_problem_ii:
{
{\bf \chinese{cex@mproblem}、填空题}（本题共\cexam_problem:{}小题,每空\cexam_score_i:{}分，共\cexam_score:{}分。）
}
\cs_new:Nn \cexam_problem_iii:
{
   {\bf \chinese{cex@mproblem}、判断题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。正确：$\times$ ,错误：$\surd$）
}
\cs_new:Nn \cexam_problem_iv:
{
{\bf \chinese{cex@mproblem}、计算题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。）
}
\NewDocumentCommand \question{ o o o m}
{
   \stepcounter{cex@mproblem}
   \dim_set:Nn \cexam_pb_indent_dim {1.8\ccwd}
   \dim_set:Nn \cexam_problem_dim {\linewidth}
   \dim_sub:Nn \cexam_problem_dim {\cexam_pb_indent_dim}
   \cs_set:Nn \cexam_problem: {#1}
   \cs_set:Nn \cexam_score: {#2}
   \cs_set:Nn \cexam_score_i: {#3}
   \parindent=0pt
   \parshape=~2~0pt~\linewidth~\cexam_pb_indent_dim~\cexam_problem_dim
   \str_if_in:nnTF {#4}{choice}
   {\cexam_problem_i:}
   {
      \str_if_in:nnTF {#4}{blank}
      {\cexam_problem_ii:}
      {
	 \str_if_in:nnTF {#4}{judgement}
	 {\cexam_problem_iii:}
	 {
	    \str_if_in:nnTF {#4}{calculation}
	    {\cexam_problem_iv:}
	    {{\bf \chinese{cex@mproblem}、}#4}
	 }
      }
   }
}
%% End of file \postgraduateiii.sty'.
